package com.msk.framework.web.service;

import com.google.common.collect.Lists;
import com.msk.common.constant.CacheConstants;
import com.msk.common.constant.Constants;
import com.msk.common.core.domain.R;
import com.msk.common.core.redis.RedisCache;
import com.msk.common.utils.StringUtils;
import com.msk.common.utils.uuid.IdUtils;
import com.msk.sys.domain.Office;
import com.msk.sys.domain.Role;
import com.msk.sys.domain.User;
import com.msk.sys.service.OfficeService;
import com.msk.sys.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 系统接口注册服务
 * @Author zlf
 * @Date 2023/5/11 19:08
 */
@Service
public class SysApiRegisterService {

    @Autowired
    private SystemService systemService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private SysMpLoginService loginService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 用户注册
     * @param user
     * @return
     */
    public R<?> userRegister(User user) {
        R<?> result;
        String lockKey = CacheConstants.OTHER_CACHE + "register:" + user.getMobile();
        String fastSimpleUUID = IdUtils.fastSimpleUUID();
        long expireTime = 10*1000;
        try {
            boolean lockFlag = redisCache.getLock(lockKey, fastSimpleUUID, expireTime);
            if (!lockFlag) {
                result = R.appFail(2003, "请勿重复操作");
                return result;
            }
            String mobile = user.getMobile();
            boolean mobileExist = systemService.checkMobileExist(mobile);
            String password = user.getPassword();
            String userIdentity = user.getUserIdentity();
            if (!mobileExist) {
                //如果用户不存在则默认密码为123456Aa_
                if (StringUtils.isEmpty(password)) {
                    password = "123456Aa_";
                }
                //设置手机号默认用户名
                if (StringUtils.isEmpty(user.getLoginName())) {
                    user.setLoginName(mobile);
                }
                //设置手机号默认电话
                if (StringUtils.isEmpty(user.getPhone())) {
                    user.setPhone(mobile);
                }
                user.setPassword(SystemService.entryptPassword(password));
                Office parent = new Office();
                parent.setId("0");
                Office office = new Office();
                office.setParent(parent);
                office.setType("");
                List<Office> officeList = officeService.findListOnly(office);
                if (officeList != null && officeList.size() != 0) {
                    office = officeList.get(0);
                    if (office != null && office.getId() != null) {
                        user.setOffice(office);
                        user.setCompany(office);
                    } else {
                        Office office1 = new Office();
                        office1.setId("1");
                        user.setOffice(office1);
                        user.setCompany(office1);
                    }
                }
                Role r = new Role();
                if ("1".equals(userIdentity)) {
                    r = systemService.getRoleByEnname("driver");
                } else if ("2".equals(userIdentity)) {
                    r = systemService.getRoleByEnname("goodsMember");
                } else if ("3".equals(userIdentity)) {
                    r = systemService.getRoleByEnname("common carrier");
                }
                List<Role> roleList = Lists.newArrayList();
                roleList.add(r);
                user.setRoleList(roleList);
                user.setLoginFlag("y");//是否允许登录
                user.setAccountType("1"); //注册默认账单为主账户
                user.setUserType("1");//用户类型为注册用户
                user.setNo(user.getMobile());//设置手机号默认工号
                systemService.saveNewUser(user);
            }
            result = loginService.passwordLogin(mobile, password, userIdentity);
            if (result.getCode() == Constants.SUCCESS_APP) {
                return R.appOk(result.getData(), "注册成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = R.appFail(2000, "注册失败");
        } finally {
            redisCache.releaseLock(lockKey, fastSimpleUUID);
        }
        return result;
    }
}
